import time
from py_page.base_page import BasePage
from common.log import logger


class MainPage(BasePage):
    # yaml_path = r"D:\PythonCoding\需要敲代码框架\32\WebUIFram\py_yaml\main_page.yaml"
    yaml_path = BasePage.get_yaml_path("main_page.yaml")

    def goto_login_page(self):  # 跳转到登录页
        # 伪代码
        # print("在首页点击登录按钮，跳转到登录页")
        # 真实的代码
        # 在首页点击登录按钮，跳转登录页面
        # # denglu = self.driver.find_element(By.LINK_TEXT, "登录")
        # print(denglu)
        # denglu.click()
        # self.find_and_click("LINK_TEXT", "登录")
        data = self.run_steps(self.yaml_path, "goto_login_page")

        logger.info("【---跳转到登录页---】")
        from py_page.login_page import LoginPage
        return LoginPage(self.driver)

    def get_user_name(self):  # 获取用户名
        user_name = self.run_steps(self.yaml_path, "get_user_name").text
        logger.info(f"在首页获取到的用户名是  {user_name}")
        return user_name

    def search_goods(self, goods, sleep):  # 搜索商品
        self.run_steps(self.yaml_path, "search_goods", goods=goods, sleep=sleep)
        return self

    def get_search_result(self):  # 获取搜索结果
        goods_list = self.run_steps(self.yaml_path, "get_search_result")
        goods = [goods.text for goods in goods_list]
        logger.info(f"搜索结果是 {goods}")
        return goods
        # temp = []
        # for goods in goods_list:
        #     goods_name = goods.text
        #     temp.append(goods_name)

    def goto_goods_detail(self, index):
        self.run_steps(self.yaml_path, "goto_goods_detail", index=index)
        time.sleep(3)
        from py_page.goods_detail import GoodsDetail
        return GoodsDetail(self.driver)


if __name__ == '__main__':
    # MainPage().goto_login_page()   LoginPage()
    MainPage().goto_login_page()
    # name = MainPage("11").get_user_name()
# {'goto_login_page': ['第一步需要的数据，调用的定位交互方法，参数'], 'get_user_name': ['第一步需要的数据，调用的定位交互方法，参数'], 'search_goods': ['第一步需要的数据，调用的定位交互方法，参数'], 'get_search_goods_result': ['第一步需要的数据，调用的定位交互方法，参数']}
